package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.domain.MemberProductCollection;
import com.macro.mall.portal.repository.MemberProductCollectionRepository;
import com.macro.mall.portal.service.MemberCollectionService;
import com.macro.mall.portal.service.UmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员收藏Service实现类
 * Created by macro on 2018/8/2.
 */
@Service
public class MemberCollectionServiceImpl implements MemberCollectionService {
    @Autowired
    private MemberProductCollectionRepository productCollectionRepository;
    @Autowired
    private UmsMemberService memberService;

    @Override
    public int add(MemberProductCollection productCollection) {
        int count = 0;
        UmsMember member = memberService.getCurrentMember();
        productCollection.setMemberId(member.getId());
        productCollection.setMemberUsername(member.getUsername());
        productCollection.setMemberIcon(member.getIcon());
        MemberProductCollection findCollection = productCollectionRepository.findByMemberIdAndProductId(productCollection.getMemberId(), productCollection.getProductId());
        if (findCollection == null) {
            productCollectionRepository.save(productCollection);
            count = 1;
        }
        return count;
    }

    @Override
    public int delete(String productId) {
        int count = 0;
        UmsMember member = memberService.getCurrentMember();
        if(productId.contains(",")){
            String[] split = productId.split(",");
            for (int i = 0; i < split.length; i++) {
                Long l = Long.parseLong(split[i]);
                int i1 = productCollectionRepository.deleteByMemberIdAndProductId(member.getId(), l);
                count += i1;
            }
        }else {
            Long id = Long.parseLong(productId);
            int i = productCollectionRepository.deleteByMemberIdAndProductId(member.getId(), id);
            count += i;
        }
        return count;
    }

    @Override
    public Page<MemberProductCollection> list(Integer pageNum, Integer pageSize) {
        UmsMember member = memberService.getCurrentMember();
        List<MemberProductCollection> allByMemberId = productCollectionRepository.findAllByMemberId(member.getId());
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return productCollectionRepository.findByMemberId(member.getId(), pageable);
    }

    @Override
    public List<MemberProductCollection> getList(Long memberId, Integer type, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
//        UmsMember member = memberService.getCurrentMember();
        List<MemberProductCollection> all = productCollectionRepository.findAllByMemberId(memberId);
        CommonPage<MemberProductCollection> page = CommonPage.restPage(all);
        List<MemberProductCollection> list = page.getList();
        List<MemberProductCollection> collect = list.stream().filter(e -> e.getProductType().equals(type)).collect(Collectors.toList());
        CommonPage<MemberProductCollection> result = new CommonPage<>();
        BeanUtils.copyProperties(page, result);
        result.setList(collect);
        return collect;
    }

    @Override
    public MemberProductCollection detail(Long productId) {
//        MemberProductCollection memberProductCollection = new MemberProductCollection();
        UmsMember member = memberService.getCurrentMember();
        if(member!=null&&member.getId()!=null&&productId!=null){
            MemberProductCollection memberProductCollection = productCollectionRepository.findByMemberIdAndProductId(member.getId(), productId);
              return memberProductCollection;
        }
        return null;
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        productCollectionRepository.deleteAllByMemberId(member.getId());
    }
}
